<?php
namespace app\manage\controller;

use app\manage\controller\Common;

class GoodsController extends CommonController{

	/**
	 * 实名认证列表
	 */
	public function get_list(){
		$statusArr=$this->getStatus();
		if (request()->isAjax()) {
			$param = request()->param();

			$param['limit']     = (isset($param['limit']) and $param['limit']) ? $param['limit'] : 15; // 每页记录数
			$param['page']      = (isset($param['page']) and $param['page']) ? $param['page'] : 1; // 当前页
			$limitOffset        = ($param['page'] - 1) * $param['limit']; // 偏移量
			$param['sortField'] = (isset($param['sortField']) && $param['sortField']) ? $param['sortField'] : 'a.id';
			$param['sortType']  = (isset($param['sortType']) && $param['sortType']) ? $param['sortType'] : 'desc';

            $where="a.status!=-1 and a.total_number>0 ";
            //用户名
			if(isset($param['username']) && $param['username']){
                $where.=" and b.username like '%{$param['username']}%'";
			}
			//uid
			if(isset($param['uid']) && $param['uid']){
                $where.=" and a.uid = {$param['uid']} ";
			}
            //状态
			if(isset($param['status']) && $param['status']){
                $where.=" and a.status = {$param['status']} ";
			}

			// 时间
			if(isset($param['datetime_range']) && $param['datetime_range']){
				$dateTime = explode(' - ', $param['datetime_range']);
				$where.=' and a.create_time >='.strtotime($dateTime[0])." and a.create_time <=".strtotime($dateTime[1]);
			}



			//查询符合条件的数据
			$data = db('users_goods')
            ->alias('a')
            ->field('a.*,b.username,n.name as nname,n.file,n.credit_type')
            ->join('users b','a.uid=b.id','left')
            ->join('nft n','a.nft_id=n.id','left')
            ->join('task tt','tt.nft_id=n.id and tt.uid=n.uid','left')
            ->where($where)
            ->order($param['sortField'], $param['sortType'])->limit($limitOffset, $param['limit'])
            ->paginate($param['limit'])
			->toArray();


			$creditType=CreditType();
			foreach ($data['data'] as $key => &$value) {
				$file=$value['file']?explode(',',$value['file']):[];
                $file_img='';
                foreach($file as $v){
					$file_img.=" <div class='layer-photos-demo'><img layer-pid='' layer-src='{$v}' src='{$v}' alt='文件' style='width:60px;height:60px';></div>";
                }
				$value['file_img']=$file_img;

				$value['create_time']=date('Y-m-d',$value['create_time']);

				$value['statusStr']=$statusArr[$value['status']];
                $value['credit_type_str']= getCreditTypeName($value['credit_type']);
			}

			//权限查询
			// if ($count) $data['power'] = model('ManageUserRole')->getUserPower(['uid'=>session('manage_userid')]);

			return json([
				'code'  => 0,
				'msg'   => '',
				'count' => $data['total'],
				'data'  => $data['data']
			]);
		}


		$this->assign('statusArr',$statusArr);
		return view();
	}

	/**
	 * 编辑
	 * @return [type] [description]
	 */
	public function edit(){
        $id=request()->param('id',0,'intval');
		if(request()->isAjax()){
            $param = input('param.');
            $goods = model('goods')->where("id",$param['id'])->find();
            $nft = model('nft')->where("id",$goods['nft_id'])->find();
            $nftArray = array(
                "name"=>$param['name'],
                "file"=>$param['file'],
                "credit_type"=>$param['credit_type'],
                "uid"=>$param['nuid'],
                "content"=>$param['content'],
                "characteristic"=>$param['characteristic'],
                "reward_price"=>$param['reward_price'],
                "release_price"=>$param['release_price'],
                "recovery_price"=>$param['recovery_price'],
            );
            model('nft')->where('id', $goods['nft_id'])->update($nftArray);
            $taskArray = array(
                "total_number"=>$param['total_number'],
                "uid"=>$param['nuid'],
                "person_time"=>$param['person_time'],
                "reward_price"=>$param['reward_price'],
                "release_price"=>$param['release_price'],
                "recovery_price"=>$param['recovery_price'],
            );
            $taskWhere = array(
              "nft_id"=>$goods['nft_id'],
              "uid"=>$nft['uid'],
            );
            model('Task')->where($taskWhere)->update($taskArray);
            ///总数量？？？？？
            $goodsArray = array(
//                "total_number"=>$param['total_number'],
                "reward_price"=>$param['reward_price'],
                "release_price"=>$param['release_price'],
                "recovery_price"=>$param['recovery_price'],
            );
            model('goods')->where('id', $param['id'])->update($goodsArray);
            return true;
		}
        $data= model('goods')
            ->alias('a')
            ->field('a.*,b.username,n.name as nname,n.file,n.credit_type,n.uid as nuid,
            n.characteristic,n.content as ncontent,tt.total_number as ntotal_number,tt.person_time')
            ->join('users b','a.uid=b.id','left')
            ->join('nft n','a.nft_id=n.id','left')
            ->join('task tt','tt.nft_id=n.id and tt.uid=n.uid','left')
            ->find($id);
//        $users=db('users')->field('id,username')->select();
        $users=db('users')->field('id,username')->where(['user_type'=>4])->select();
        $creditType=CreditType();

        $this->assign('creditType',$creditType);
        $this->assign('users',$users);
        $this->assign('data',$data);

		return $this->fetch();
	}
	/**
	 * Undocumented function
	 *
	 * @return void
	 */
	private function getStatus(){
		return [
			1=>'未合成',
			2=>'已合成',
		];
	}

}
